package com.ruoyi.samplemod.mapper;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.samplemod.domain.entity.Sample;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ACBSampleMapper {



    @Select("select * from sample where bu='AC1' ")
    @Results(id="sampleResultMap",value = {
            @Result(property = "id", column = "id"),
            @Result(property = "sampleName", column = "sample_name"),
            @Result(property = "sampleCode", column = "sample_code"),
            @Result(property = "validityPeriod", column = "validity_period"),
            @Result(property = "enterTime", column = "enter_time"),
            @Result(property = "lastTime", column = "end_time"),
            @Result(property = "user", column = "user"),
            @Result(property = "sysDept", column = "sys_deptId", javaType = SysDept.class ,one = @One(select = "com.ruoyi.system.mapper.SysDeptMapper.selectDeptById")),
            @Result(property = "reviewerZ", column = "reviewerId_z"),
            @Result(property = "reviewerJ", column = "reviewerId_j"),
            @Result(property = "reviewerP", column = "reviewerId_p"),
            @Result(property = "reviewerF", column = "reviewerId_f"),
            @Result(property = "bu", column = "bu")
    })
    List<Sample> selectSample();

    @Select({
            "<script>" +
                    "SELECT * FROM sample " +
                    "<where>" +
                    " bu='AC1' "+
                    "   <if test='sampleCode != null and sampleCode != \"\"'> and sample_code like CONCAT('%',#{sampleCode},'%') </if>" + // 区分大小写
                    "   <if test='queryTimeStart != \"\" and queryTimeEnd != \"\"'> and enter_time between #{queryTimeStart} and #{queryTimeEnd} </if>" +
                    "</where>" +
                    "</script>"
    })
    @ResultMap("sampleResultMap")
    List<Sample> selectSampleByDataByName(@Param("queryTimeStart")String queryTimeStart,@Param("queryTimeEnd")String queryTimeEnd,@Param("sampleCode") String sampleCode);

    @Insert("INSERT INTO sample(sys_deptId, sample_name, sample_code, reviewerId_z, reviewerId_j, reviewerId_p,reviewerId_f, validity_period, enter_time, end_time, user,bu) " +
            "VALUES(#{sysDeptId}, #{sampleName}, #{sampleCode}, #{reviewerZ}, #{reviewerJ}, #{reviewerP},#{reviewerF}, #{validityPeriod}, #{enterTime}, #{lastTime}, #{user},'AC1')")
    int addSample(Sample sample);

    @Delete("DELETE from sample where Id=#{id}")
    Long delSample(@Param("id")Long id);

    @Select("select count(sample_code) from sample where sample_code=#{sample_code}")
    int sampleNameisExist(@Param("sample_code") String sampleCode);

    @Select("select * from sample where bu='AC1' order by sys_deptId")
    @ResultMap("sampleResultMap")
    List<Sample> selectSampleAllOrderByDept();
}
